//
//  ZJRZViewController_Detail.m
//  Estate
//
//  Created by heyong on 16/6/24.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "Tweet.h"
#import "SafeDetailViewController.h"
#import "PatrolDetailViewController.h"
#import "GJDBViewController_Detail.h"
#import "SGFDBDetailViewController.h"
#import "DefaineCell.h"
#import "B_wuLiaoCell.h"
#import "JLRJTitleCell.h"
#import "H1_ImagesCell.h"

#import "ZJRZViewController_Detail.h"


@interface ZJRZViewController_Detail ()<UITableViewDelegate,UITableViewDataSource>
{
    DBHelper *_helper;
}

/**tableView*/
@property (strong ,nonatomic) TPKeyboardAvoidingTableView*tableView;

/**DB*/
@property (strong ,nonatomic)FMDatabase *db;

/**数据源*/
@property (strong ,nonatomic)NSMutableArray *dataArray;

//附件
@property (strong, nonatomic) Tweet *curTweet;

//显示的图片url链接
@property (strong, nonatomic)NSMutableArray *selectedAssetURLs;

//删除图片的url链接
@property (strong, nonatomic)NSMutableArray *delImagrUrlArray;

//标题array
@property(strong, nonatomic)NSMutableArray *titArray;

@property (copy, nonatomic) NSString *content;


@end


@implementation ZJRZViewController_Detail

- (NSMutableArray *)titArray{
    if(!_titArray){
        _titArray=[NSMutableArray array];
    }
    return _titArray;
}

-(NSMutableArray *)dataArray{
    if(_dataArray==nil){
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)selectedAssetURLs{
    if(!_selectedAssetURLs){
        _selectedAssetURLs=[NSMutableArray array];
    }
    return _selectedAssetURLs;
}

- (NSMutableArray *)delImagrUrlArray{
    if(!_delImagrUrlArray){
        _delImagrUrlArray=[NSMutableArray array];
    }
    return _delImagrUrlArray;
}

- (FMDatabase *)db{
    if(_db==nil){
        _db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    }
    return _db;
}

- (TPKeyboardAvoidingTableView *)tableView{
    if(_tableView==nil){
        _tableView=[[TPKeyboardAvoidingTableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:@"DefaineCell" bundle:nil] forCellReuseIdentifier:@"deCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"B_wuLiaoCell" bundle:nil] forCellReuseIdentifier:@"tCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"JLRJTitleCell" bundle:nil] forCellReuseIdentifier:@"titleCell"];
        [_tableView registerClass:[H1_ImagesCell class] forCellReuseIdentifier:kCellIdentifier_H1_ImagesCell];
    }
    return _tableView;
}

- (void)setLogId:(NSString *)logId {
    if(logId!=nil){
        _logId=logId;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"日志";
    // 添加右上方提交按钮
    if([self.state isEqualToString:@"待审阅"]){
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wancheng"] style:UIBarButtonItemStyleDone target:self action:@selector(clickSave)];
    }
    _curTweet = [[Tweet alloc] init];
    _helper = [DBHelper help];
    [self setData];
    [self.view addSubview:self.tableView];
}

// 刷新附件
- (void)refresh:(id)sender {
    if(self.selectedAssetURLs!=nil){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //设置图片
            self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
            //回到主线程来刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:self.dataArray.count]] withRowAnimation:UITableViewRowAnimationFade];
            });
        });
    }
}

//!!!!:保存视图
- (void)clickSave {
    if(self.content==nil||[self.content isEqualToString:@""]){
        [LSUtility showToastWithMessage:@"请输入意见" inView:self.view Andtime:1.];
        
    }else{
        [self reView];
    }
}

#pragma mark -审阅完成
- (void)reView {
    NSDictionary *dic=@{@"REVIEW_STATE":@"1",@"REVIEW_TIME":[LSUtility nowTime],@"REVIEW_INFO":self.content,@"LOCAL_MODIFY_STATE":@"MOD"};
    [_helper DataBase:self.db updateTable:WORK_LOG setKeyValues:dic whereCondition:@{@"ID":self.logId}];
    
    NSNotification *notice = [NSNotification notificationWithName:@"Refresh_zj_rz" object:nil userInfo:nil];
    [[NSNotificationCenter defaultCenter]postNotification:notice];
    
    // 删除的图片 附件表设置为del
    for (NSString *urlStr in self.delImagrUrlArray) {
        NSArray *subArr=[urlStr componentsSeparatedByString:@"/"];
        NSString *imgName=[subArr lastObject];
        BOOL del= [LSUtility delAttachmentForFileId:_logId fileName:imgName];
        if(del){
            NSLog(@"修改成功");
        }else{
            NSLog(@"修改失败");
        }
    }
    
    // 通知首页刷新数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"home" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 设置数据
- (void)setData {
    NSDictionary *tabKey=@{@"PRO_ID":@"text",
                           @"LOG_TYPE":@"text",
                           @"LOG_USERS":@"text",
                           @"CONSTRACT_PROGRESS_INFO":@"text",
                           @"DAY_SUPERVISION_WORK_INFO":@"text",
                           @"OTHER_SUPERVISION_INFO":@"text",
                           @"SAFE_PATROL_INFO":@"text",
                           @"SAFE_FILE_REVIEW_INFO":@"text",
                           @"SAFE_MEETING_INFO":@"text",
                           @"PROBLEM_DEAL_INFO":@"text",
                           @"REVIEW_INFO":@"text",
                           @"REVIEW_USERS":@"text",
                           @"REVIEW_SCORE":@"text",
                           @"REVIEW_STATE":@"text",
                           @"LOG_DESC":@"text",
                           @"Create_User_name":@"text",
                           @"CREATE_TIME":@"text",
                           //迭代新数据
                           @"JIANCHA_INFO":@"text",
                           @"ZHILIANG_INFO":@"text",
                           @"JINDU_INFO":@"text",
                           @"CAILIAO_INFO":@"text",
                           @"YINBI_INFO":@"text",
                           @"SHOUFAWEN_INFO":@"text",
                           @"JIANZHENG_INFO":@"text",
                           @"JIANZHENG_INFO":@"text",
                           @"HUIYI_INFO":@"text",
                           @"PATROL_RECORD_INFO":@"text",
                           @"SAFE_RECORD_INFO":@"text",
                           };
    
    NSDictionary *dataDic = [[_helper DataBase:self.db selectKeyTypes:tabKey
                                    fromTable:WORK_LOG
                               whereCondition:@{@"ID":_logId}]lastObject];
    NSLog(@"%@",dataDic);
    //第一组数据
    NSMutableArray *sectionArr1=[NSMutableArray array];
    //项目
    NSString *proName=[LSUtility getProjectNameByProjectId:dataDic[@"PRO_ID"]][@"PROJECT_NAME"];
    [sectionArr1 addObject:proName];
    //创建人
    if(![dataDic[@"Create_User_name"] isEqualToString:@"admin"]){
        [dataDic setValue:[LSUtility getuserNameByRealName:dataDic[@"Create_User_name"] ][@"REAL_NAME"]forKey:@"Create_User_name"];
    }
    [sectionArr1 addObject:DEFISNUL(dataDic[@"Create_User_name"])];
    //时间
    NSString *time=dataDic[@"CREATE_TIME"];
    [sectionArr1 addObject:time];
    //安全日志
    if ([self.type isEqualToString:@"LOG_02"]) {
        //安全巡视情况 数组接受
        NSString *jsonSafePatrol= dataDic[@"SAFE_PATROL_INFO"];
        [self addDataWithjsonString:jsonSafePatrol sectionArray:sectionArr1 title:@"安全巡视情况"];
        // 安全情况
        NSString *safe = dataDic[@"SAFE_RECORD_INFO"];
        [sectionArr1 addObject:DEFISNUL(safe)];
        [self.titArray addObject:@"安全情况"];
        
    } else if ([self.type isEqualToString:@"LOG_01"])    //监理日志
    {
        //巡视记录情况
        NSString *jsonXunshi=dataDic[@"CONSTRACT_PROGRESS_INFO"];
        [self addDataWithjsonString:jsonXunshi sectionArray:sectionArr1 title:@"巡视记录情况"];
        //检查记录情况
        NSString *jiancha=dataDic[@"JIANCHA_INFO"];
        [self addDataWithjsonString:jiancha sectionArray:sectionArr1 title:@"检查记录情况"];
        //质量记录情况
        NSString *zhiliang=dataDic[@"ZHILIANG_INFO"];
        [self addDataWithjsonString:zhiliang sectionArray:sectionArr1 title:@"质量记录情况"];
        //进度记录情况
        NSString *jindu=dataDic[@"JINDU_INFO"];
        [self addDataWithjsonString:jindu sectionArray:sectionArr1 title:@"进度记录情况"];
        //材料进场记录情况
        NSString *cailiao=dataDic[@"CAILIAO_INFO"];
        [self addDataWithjsonString:cailiao sectionArray:sectionArr1 title:@"材料进场记录情况"];
        //隐蔽检查验收记录情况
        NSString *yinbi = dataDic[@"YINBI_INFO"];
        [self addDataWithjsonString:yinbi sectionArray:sectionArr1 title:@"隐蔽检查验收记录情况"];
        //收发文记录情况
        NSString *sfw=dataDic[@"SHOUFAWEN_INFO"];
        [self addDataWithjsonString:sfw sectionArray:sectionArr1 title:@"收发文记录情况"];
        //见证取样记录情况
        NSString *jz=dataDic[@"JIANZHENG_INFO"];
        [self addDataWithjsonString:jz sectionArray:sectionArr1 title:@"见证取样记录情况"];
        //会议及协调记录情况
        NSString *hy=dataDic[@"HUIYI_INFO"];
        [self addDataWithjsonString:hy sectionArray:sectionArr1 title:@"会议及协调记录情况"];
        //旁站记录情况
        NSString *work=dataDic[@"DAY_SUPERVISION_WORK_INFO"];
        [self addDataWithjsonString:work sectionArray:sectionArr1 title:@"旁站记录情况"];
        //其他监理情况
        NSString *otherW=dataDic[@"OTHER_SUPERVISION_INFO"];
        [self addDataWithjsonString:otherW sectionArray:sectionArr1 title:@"其他监理情况"];
        // 巡视情况
        NSString *patrol = dataDic[@"PATROL_RECORD_INFO"];
        [self addDataWithjsonString:patrol sectionArray:sectionArr1 title:@"巡视情况"];
    }
    
    [self.dataArray addObject:sectionArr1];
    
    //问题及处理情况
    //这儿要改动， PROBLEM_DEAL_INFO 由json变为字符串
    NSString *wen=dataDic[@"PROBLEM_DEAL_INFO"];
    NSArray *array=[LSUtility dictionaryWithJsonString:wen];
    if(array!=nil){
        [self.dataArray addObject:array];
    }else{
        [self.dataArray addObject:@[@""]];
    }
    
    //第三组数据
    NSMutableArray *sectionArr3=[NSMutableArray array];
    //说明
    NSString *instructions=dataDic[@"LOG_DESC"];
    [sectionArr3 addObject:instructions];
    //审阅人
    NSString *sheny=[LSUtility getuserIdByUserName:dataDic[@"REVIEW_USERS"]][@"REAL_NAME"];
    [sectionArr3 addObject:DEFISNUL(sheny) ];
    //评价
    NSString *pingj=dataDic[@"REVIEW_INFO"];
    [sectionArr3 addObject:pingj];
    [self.dataArray addObject:sectionArr3];
    //查找附件
    NSArray *imageArr=[LSUtility queryAllAttachment:self.logId];
    //查询附件表
    if(imageArr.count>0){
        for (NSDictionary *imageDic in imageArr) {
            NSString *ID=imageDic[@"ID"];
            NSString *fileName=[LSUtility queryAttachment:ID][@"FILE_NAME"];
            NSString *filePath=[DEF_FILE_PATH stringByAppendingString:
                                [NSString stringWithFormat:@"/%@",fileName]];
            [self.selectedAssetURLs addObject:filePath];
        }
    }
    self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
    [self refresh:nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //多返回一组 附件组
    return self.dataArray.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        //分组加上 项目、填写人、时间
        return self.titArray.count+3;
        
    } else if (section==1) {
        NSArray *arr = self.dataArray[1];
        //安全日志
        if([self.type isEqualToString:@"LOG_02"]){
            if([[arr firstObject] isKindOfClass:[NSDictionary class]]){
                
            }else if([[arr firstObject] isKindOfClass:[NSString class]]){
                if([[arr firstObject] isEqualToString:@""]){
                    return 0;
                }
            }
        //监理日志
        }else{
            if([[arr firstObject] isKindOfClass:[NSDictionary class]]){
                
            }else if([[arr firstObject] isKindOfClass:[NSString class]]){
                if([[arr firstObject] isEqualToString:@""]){
                    return 0;
                }
            }
        }
        return arr.count;
        
    //说明 审阅人 评价
    }else  if (section==2){
        return 3;
        
    }else{
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    long section=indexPath.section;
    long row=indexPath.row;
    if(section==0){
        // //项目、 填写人、时间
        if(row==1||row==2||row==0){
            return 44;
        }else{
            return 150;
        }
    }else if(section==2){
        //审阅人
        if(row==1){
            return 44;
        }else{
            //说明和评价
            return 150;
        }
    }else if(section==1) {
        return 40;
        
    }else{  //附件
        return  [H1_ImagesCell cellHeightWithObj:_curTweet title:@"附件信息"];
    }
}

//返回数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    long row=indexPath.row;
    long section=indexPath.section;
    if (section==0) {
        NSArray *sectionArr1=weakSelf.dataArray[section];
        if (row < 3) {
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
            if (row == 0) {
                [cell setTitle:@"项    目:" andDetail:sectionArr1[row] andLeftConstraint:15];
                return cell;
                
            } else if(row == 1) {
                [cell setTitle:@"填 写 人:" andDetail:sectionArr1[row] andLeftConstraint:15];
                return cell;
                
            } else {
                [cell setTitle:@"时    间:" andDetail:sectionArr1[row] andLeftConstraint:15];
                return cell;
            }
        } else {
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tCell" forIndexPath:indexPath];
            cell.isEdit = NO;
            [cell setTitleColor:[UIColor colorWithRed:0.20 green:0.64 blue:0.60 alpha:1.00] andTextColor:[UIColor colorWithRGBHex:0x787878]];
            [cell setTitle:self.titArray[row-3] andPlaceHolder:@"" andTextValue:sectionArr1[row]];
            return cell;
        }
    } else if (section==1) {
        JLRJTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titleCell" forIndexPath:indexPath];
        cell.isEdit=NO;
        cell.str=@"look";
        NSArray *section2Arr=weakSelf.dataArray[section];
        NSDictionary *dics = section2Arr[row];
        cell.title.text = section2Arr[row];
        NSString *processId = dics[@"recordId"];
        NSString *type=[self typeProcessForId:processId];
        
        //详情信息
        cell.detailBlick = ^{
            //根据id 查询最后修改时间
            NSDictionary *dic = [self modifyForId:processId];
            NSString *extend=dic[@"ADDRES_INFO"];
            NSString *beginTime=dic[@"BEGIN_TIME"];
            NSString *endTime=dic[@"END_TIME"];
            
            if ([type isEqualToString:@"1"])//督办
            {
                GJDBViewController_Detail *detail=[[GJDBViewController_Detail alloc]init];
                detail.processId=processId;
                detail.addressInfo=extend;
                detail.beginTime=beginTime;
                detail.endTime=endTime;
                [self.navigationController pushViewController:detail animated:YES];
            }
            else if ([type isEqualToString:@"2"])//跟进
            {
                SGFDBDetailViewController *GJDetail=[[SGFDBDetailViewController alloc]init];
                GJDetail.flag = @"detail";
                GJDetail.processID =processId;
                GJDetail.addressInfo=extend;
                GJDetail.beginTime=beginTime;
                GJDetail.endTime=endTime;
                GJDetail.gengjing = YES;
                [self.navigationController pushViewController:GJDetail animated:YES];
            }
            else if ([type isEqualToString:@"PATROL_01"])   // 巡视
            {
                PatrolDetailViewController *patrolVC = [[PatrolDetailViewController alloc] init];
                patrolVC.isDiary = YES;
                patrolVC.patrolModel = [self selectPatrolInformation:processId];
                [self.navigationController pushViewController:patrolVC animated:YES];
            }
            else if ([type isEqualToString:@"SAFE_RECORD_01"] || [type isEqualToString:@"SAFE_RECORD_02"] || [type isEqualToString:@"RECORD_PATROL_03"] || [type isEqualToString:@"SAFE_RECORD_04"] || [type isEqualToString:@"SAFE_RECORD_05"])    // 安全
            {
                SafeDetailViewController *safeVC = [[SafeDetailViewController alloc] init];
                safeVC.isDiary = YES;
                safeVC.safeModel = [self selectSafeInformation:processId];
                [self.navigationController pushViewController:safeVC animated:YES];
            }
        };
        return cell;
        
    } else if(section==2) {
        NSArray *sectionArr3=weakSelf.dataArray[section];
        if(row==0){
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tCell" forIndexPath:indexPath];
            if([self.jiafangQuery isEqualToString:@"jia"]){
                cell.isEdit=NO;
                
            }else{
                if([self.state isEqualToString:@"待审阅"]){
                    cell.isEdit=NO;
                }else{
                    cell.isEdit=NO;
                }
            }
            
            [cell setTitle:@"说   明:" andPlaceHolder:@"" andTextValue:sectionArr3[row]];
            cell.textValueChangedBlock= ^(NSString *values){
                self.content=values;
            };
            return cell;
            
        }else if(row==1){
            DefaineCell *cell=[tableView dequeueReusableCellWithIdentifier:@"deCell" forIndexPath:indexPath];
            [cell setTitle:@"审 阅 人:" andDetail:sectionArr3[row] andLeftConstraint:15];
            return cell;
            
        }else{
            B_wuLiaoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tCell" forIndexPath:indexPath];
            if([self.jiafangQuery isEqualToString:@"jia"]){
                cell.isEdit=NO;
            }else{
                if([self.state isEqualToString:@"待审阅"]){
                    cell.isEdit=YES;
                }else{
                    cell.isEdit=NO;
                }
            }
            
            if(self.content&&self.content.length>0){
                [cell setTitle:@"评   价:" andPlaceHolder:@"请输入评价" andTextValue:self.content];
            }else{
                [cell setTitle:@"评   价:" andPlaceHolder:@"请输入评价" andTextValue:sectionArr3[row]];
            }
            
            cell.textValueChangedBlock= ^(NSString *values){
                self.content=values;
            };
            return cell;
        }
    }else{
        WEAKSELF
        H1_ImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_H1_ImagesCell forIndexPath:indexPath];
        //甲方查看操作
        if([self.jiafangQuery isEqualToString:@"jia"]){
            cell.isEdit=NO;
            
        }else{
            cell.isNoADD=YES;
            if([self.state isEqualToString:@"待审阅"]){
                cell.isEdit=YES;
            }else{
                cell.isEdit=NO;
            }
        }
        [cell setTitle:@"附件"];
        cell.curTweet = _curTweet;
        //删除图片
        cell.deleteTweetImageBlockUrl = ^(TweetImage *toDelete,NSString *deurl){
            //移除图片
            [weakSelf.curTweet deleteATweetImage:toDelete];
            //移除数组
            [weakSelf.selectedAssetURLs removeObject:deurl];
            [weakSelf refresh:nil];
            //添加至删除数组
            [weakSelf.delImagrUrlArray addObject:deurl];
        };
        return cell;
    }
}

// 组头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 30;
    }else if(section==1){
        return 30;
    }else{
        return 0.1;
    }
}

// 组头
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section==0) {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 30)];
        view.backgroundColor=[UIColor colorWithRed:0.85 green:0.93 blue:0.97 alpha:1.00];
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, DEF_SCREEN_WIDTH-5,20 )];
        lab.backgroundColor=[UIColor colorWithRed:0.85 green:0.93 blue:0.97 alpha:1.00];
        lab.textColor=[UIColor colorWithRed:0.05 green:0.60 blue:0.55 alpha:1.00];
        if([self.type isEqualToString:@"LOG_02"]){
            lab.text=@">>安全日志审阅";
            
        }else{
            lab.text=@">>监理日志审阅";
        }
        lab.font=[UIFont systemFontOfSize:15.];
        [view addSubview:lab];
        return view;
        
    } else if (section==1) {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 30)];
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, -8, DEF_SCREEN_WIDTH, 8)];
        image.image=[UIImage imageNamed:@"juchixia"];
        [view addSubview:image];
        UILabel *lab=[[UILabel alloc]init];
        lab.font=[UIFont systemFontOfSize:15.];
        [view addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(5);
            make.right.mas_offset(5);
            make.top.mas_offset(3);
            make.height.mas_offset(20);
        }];
        lab.textColor=[UIColor colorWithRed:0.03 green:0.60 blue:0.55 alpha:1.00];
        if([self.type isEqualToString:@"LOG_02"]){
            lab.text=@"安全流程情况";
            
        }else{
            lab.text=@"问题及处理情况";
        }
        return view;
        
    } else {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 8)];
        UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, -8, DEF_SCREEN_WIDTH, 8)];
        image.image=[UIImage imageNamed:@"juchixia"];
        [view addSubview:image];
        return view;
    }
}

// 组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}

// 组尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 20)];
    view.backgroundColor=[UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0 ,0 , DEF_SCREEN_WIDTH, 8)];
    image.image=[UIImage imageNamed:@"juchishang"];
    image.backgroundColor=[UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.00];
    [view addSubview:image];
    return view;
}

// 查找巡视信息
- (PatrolRecord *)selectPatrolInformation:(NSString *)safeID {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    // 查找基本数据
    NSString *baseStr = [NSString stringWithFormat:@"SELECT * FROM PATROL_RECORD WHERE ID = '%@'", safeID];
    
    PatrolRecord *model = [[PatrolRecord alloc]init];
    FMResultSet *results = [db executeQuery:baseStr];
    while ([results next]) {
        model.id = [results stringForColumn:@"ID"];
        model.proId = [results stringForColumn:@"PRO_ID"];
        model.supervisorProUnitId = [results stringForColumn:@"SUPERVISOR_PRO_UNIT_ID"];
        model.constructProUnitId = [results stringForColumn:@"CONSTRUCT_PRO_UNIT_ID"];
        model.supervisorProUnitName = [results stringForColumn:@"SUPERVISOR_PRO_UNIT_NAME"];
        model.constructProUnitName = [results stringForColumn:@"CONSTRUCT_PRO_UNIT_NAME"];
        model.supervisorContractNumber = [results stringForColumn:@"SUPERVISOR_CONTRACT_NUMBER"];
        model.constructContractNumber = [results stringForColumn:@"CONSTRUCT_CONTRACT_NUMBER"];
        model.recordUser = [results stringForColumn:@"RECORD_USER"];
        model.recordType = [results stringForColumn:@"RECORD_TYPE"];
        model.recordTitle = [results stringForColumn:@"RECORD_TITLE"];
        model.recordContent = [results stringForColumn:@"RECORD_CONTENT"];
        model.state = [results stringForColumn:@"STATE"];
        model.recordNumber = [results stringForColumn:@"RECORD_NUMBER"];
        model.startTime = [results stringForColumn:@"START_TIME"];
        model.endTime = [results stringForColumn:@"END_TIME"];
        model.startAndEndTime = [results stringForColumn:@"START_AND_END_TIME"];
        model.weather = [results stringForColumn:@"WEATHER"];
        model.addId = [results stringForColumn:@"ADD_ID"];
        model.addInfo = [results stringForColumn:@"ADD_INFO"];
        model.rangeAndPosition = [results stringForColumn:@"RANGE_AND_POSITION"];
        model.majorproAndPerson = [results stringForColumn:@"MAJORPRO_AND_PERSON"];
        model.majorInspectData = [results stringForColumn:@"MAJOR_INSPECT_DATA"];
        model.problemAndDeal = [results stringForColumn:@"PROBLEM_AND_DEAL"];
        model.extendInfo = [results stringForColumn:@"EXTEND_INFO"];
        model.createusername = [results stringForColumn:@"CREATE_USER_NAME"];
        model.createtime = [results stringForColumn:@"CREATE_TIME"];
        model.createip = [results stringForColumn:@"CREATE_IP"];
        model.modifyusername = [results stringForColumn:@"MODIFY_USER_NAME"];
        model.modifytime = [results stringForColumn:@"MODIFY_TIME"];
        model.modifyip = [results stringForColumn:@"MODIFY_IP"];
    }
    [db close];
    return model;
}

// 查找安全信息
- (SecurityRecordBasic *)selectSafeInformation:(NSString *)safeID {
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    // 查找基本数据
    NSString *baseStr = [NSString stringWithFormat:@"SELECT * FROM SAFE_RECORD_BASIC WHERE ID = '%@'", safeID];
    
    SecurityRecordBasic *model = [[SecurityRecordBasic alloc] init];
    FMResultSet *baseResult = [db executeQuery:baseStr];
    while ([baseResult next]) {
        model.id = [baseResult stringForColumn:@"ID"];
        model.proId = [baseResult stringForColumn:@"PRO_ID"];
        model.supervisorProUnitId = [baseResult stringForColumn:@"SUPERVISOR_PRO_UNIT_ID"];
        model.constructProUnitId = [baseResult stringForColumn:@"CONSTRUCT_PRO_UNIT_ID"];
        model.supervisorProUnitName = [baseResult stringForColumn:@"SUPERVISOR_PRO_UNIT_NAME"];
        model.constructProUnitName = [baseResult stringForColumn:@"CONSTRUCT_PRO_UNIT_NAME"];
        model.supervisorContractNumber = [baseResult stringForColumn:@"SUPERVISOR_CONTRACT_NUMBER"];
        model.constructContractNumber = [baseResult stringForColumn:@"CONSTRUCT_CONTRACT_NUMBER"];
        model.recordUser = [baseResult stringForColumn:@"RECORD_USER"];
        model.recordType = [baseResult stringForColumn:@"RECORD_TYPE"];
        model.recordTitle = [baseResult stringForColumn:@"RECORD_TITLE"];
        model.recordContent = [baseResult stringForColumn:@"RECORD_CONTENT"];
        model.state = [baseResult stringForColumn:@"STATE"];
        model.recordNumber = [baseResult stringForColumn:@"RECORD_NUMBER"];
        model.supervisorOpinion = [baseResult stringForColumn:@"SUPERVISOR_OPINION"];
        model.chiefSupervisorOpinion = [baseResult stringForColumn:@"CHIEF_SUPERVISOR_OPINION"];
        model.projectContent = [baseResult stringForColumn:@"PROJECT_CONTENT"];
        model.otherInfo = [baseResult stringForColumn:@"OTHER_INFO"];
        model.weather = [baseResult stringForColumn:@"WEATHER"];
        model.addId = [baseResult stringForColumn:@"ADD_ID"];
        model.addInfo = [baseResult stringForColumn:@"ADD_INFO"];
        model.extendInfo = [baseResult stringForColumn:@"EXTEND_INFO"];
        model.createusername = [baseResult stringForColumn:@"CREATE_USER_NAME"];
        model.createtime = [baseResult stringForColumn:@"CREATE_TIME"];
        model.createip = [baseResult stringForColumn:@"CREATE_IP"];
        model.modifyusername = [baseResult stringForColumn:@"MODIFY_USER_NAME"];
        model.modifytime = [baseResult stringForColumn:@"MODIFY_TIME"];
        model.modifyip = [baseResult stringForColumn:@"MODIFY_IP"];
        model.constructContractMoney = [baseResult stringForColumn:@"CONSTRUCT_CONTRACT_MONEY"];
        model.recifyState = [baseResult stringForColumn:@"RECIFY_STATE"];
        model.problemAndDeal = [baseResult stringForColumn:@"PROBLEM_AND_DEAL"];
        model.chiefSupervisorOpinionState = [baseResult stringForColumn:@"CHIEF_SUPERVISOR_OPINION_STATE"];
    }
    [db close];
    return model;
}

// 根据processid查出类型
-(NSString *)typeProcessForId:(NSString *)recordId {
    return  [[_helper DataBase:self.db selectKeyTypes:@{@"PROCESS_TYPE":@"text"} fromTable:PROCESS whereCondition:@{@"ID":recordId}] lastObject][@"PROCESS_TYPE"];;
}

- (NSDictionary *)modifyForId:(NSString *)ids {
    DBHelper *helpers=[DBHelper help];
    NSArray *dicArr= [helpers DataBase:self.db selectKeyTypes:@{@"MODIFY_TIME":@"text",@"ADDRES_INFO":@"text",@"EXTEND_INFO":@"text",@"END_TIME":@"text",@"BEGIN_TIME":@"text"} fromTable:PROCESS whereCondition:@{@"ID":ids}] ;
    return [dicArr firstObject];
}

-(void)addDataWithjsonString:(NSString*)jsonStr sectionArray:(NSMutableArray *)sectionArray title:(NSString *)title{
    NSArray *array = [LSUtility arrayWithJsonString:jsonStr];
    if(array.count>0){
        for (int i=0; i<array.count; i++) {
            NSDictionary *dic =array[i];
            [sectionArray addObject:DEFISNUL(dic[@"info"])];
            if (i==0) {
                [self.titArray addObject:title];
            }else{
                [self.titArray addObject:@""];
            }
        }
    }else{
        [sectionArray addObject:@""];
        [self.titArray addObject:title];
    }
}
@end

